package com.hundred.common;

import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.apache.commons.lang.StringUtils.trim;

import com.alibaba.citrus.service.form.Field;
import com.alibaba.citrus.service.form.Group;

public class GroupUtils{

    private final Group group;

    public GroupUtils(Group group) {
        this.group = group;
    }

    public void setMessage(String fieldName, String msgId) {
        Field field = group.getField(fieldName);
        if (field != null) {
            field.setMessage(msgId);
        }
    }

    public void setValue(String fieldName, String val) {
        Field field = group.getField(fieldName);
        if (field != null) {
            field.setValue(val == null ? "" : val);
        }
    }

    public void setObjectValue(String fieldName, Object val) {
        Field field = group.getField(fieldName);
        if (field != null) {
            field.setValue(val);
        }
    }

    public String getDisplayName(String fieldName) {
        Field field = group.getField(fieldName);
        if (field == null) {
            return "";
        }
        return group.getField(fieldName).getDisplayName();
    }

    public String getString(String fieldName) {
        Field field = group.getField(fieldName);
        if (field == null) {
            return "";
        }
        return defaultIfEmpty(trim(field.getStringValue()), "");
    }

    public String getString(String fieldName, String defaultVal) {
    	checkState(isNotBlank(defaultVal));
        Field field = group.getField(fieldName);
        if (field == null) {
            return defaultVal;
        }
        return defaultIfEmpty(trim(field.getStringValue(defaultVal)), "");
    }

    public long getLong(String fieldName) {
        Field field = group.getField(fieldName);
        if (field == null) {
            return 0;
        }
        return field.getLongValue();
    }

    public long getLong(String fieldName, long defaultVal) {
        Field field = group.getField(fieldName);
        if (field == null) {
            return defaultVal;
        }
        return field.getLongValue(defaultVal);
    }

    public int getInt(String fieldName) {
        Field field = group.getField(fieldName);
        if (field == null) {
            return 0;
        }
        return field.getIntegerValue();
    }

    public int getInt(String fieldName, int defaultVal) {
        Field field = group.getField(fieldName);
        if (field == null) {
            return defaultVal;
        }
        return field.getIntegerValue(defaultVal);
    }

    public Group getGroup() {
        return group;
    }

    public boolean isValidated() {
        return group.isValidated();
    }
    
    private void checkState(boolean expr) {
        if (!expr) {
            throw new IllegalStateException();
        }
    }
    
}
